import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/status/data/share_data.dart';

// 额外定义一个StatefulWidget类型的组件来包裹_InheritedWidget组件，使其可以具有状态更新功能
class ShareUpdateWidget extends StatefulWidget {
  final ShareData shareData; // 共享数据
  final Widget child;

  ShareUpdateWidget({Key? key, required this.shareData, required this.child});

  @override
  State<ShareUpdateWidget> createState() => _ShareaUpdateWidgetState();

  static ShareData of(BuildContext context) {
    final _InheritedWidget? inheritedConfig =
        context.dependOnInheritedWidgetOfExactType<_InheritedWidget>();
    return inheritedConfig!.data;
  }

  //组件刷新的方法
  static void update(BuildContext context) {
    final _InheritedWidget? inheritedConfig =
        context.dependOnInheritedWidgetOfExactType<_InheritedWidget>();
    return inheritedConfig!.state.setState(() {});
  }
}

class _ShareaUpdateWidgetState extends State<ShareUpdateWidget> {
  @override
  Widget build(BuildContext context) {
    return _InheritedWidget(
        state: this, data: widget.shareData, child: widget.child);
  }
}

class _InheritedWidget extends InheritedWidget {
  _InheritedWidget(
      {Key? key,
      required Widget child,
      required this.state,
      required this.data})
      : super(key: key, child: child);

  final _ShareaUpdateWidgetState state;
  final ShareData data;

  @override
  bool updateShouldNotify(_InheritedWidget oldWidget) {
    //print(oldWidget.data.nickname);
    //print(data.nickname);
    //return oldWidget.data.nickname != data.nickname;

    return true;
  }
}
